package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.scity.beens.PostResponse;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegActivity extends AppCompatActivity {
    EditText edtusername;
    EditText edtpassword1;
    EditText edtpassword2;
    EditText edtnick;
    EditText edtuserid;
    EditText edtphone;
    EditText edtEmail;
    RadioGroup radioGroup;
    Button button;
    RadioButton radioButton;
    PostResponse postResponse;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what ){
                case 0:
                    break;
                case 1:
                    Toast.makeText(RegActivity.this,postResponse.msg,Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        InitViews();
    }

    private void InitViews() {
        edtusername = findViewById(R.id.reg_username);
        edtpassword1 = findViewById(R.id.reg_psw1);
        edtpassword2 = findViewById(R.id.reg_psw2);
        edtnick = findViewById(R.id.reg_nick);
        edtuserid = findViewById(R.id.reg_id);
        edtphone = findViewById(R.id.reg_phone);
        radioGroup = findViewById(R.id.reg_radiogroup);
        button = findViewById(R.id.reg_post);
        edtEmail = findViewById(R.id.reg_email);
        radioButton = findViewById(R.id.reg_nan);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //准备好注册信息
                Map<String,String>map = new HashMap<String,String>();//List ArrayList
                map.put("avatar","");
                map.put("userName",edtusername.getText().toString());
                map.put("password",edtpassword1.getText().toString());
                map.put("nickName",edtnick.getText().toString());
                map.put("phonenumber",edtphone.getText().toString());
                map.put("email",edtEmail.getText().toString());
                map.put("idCard",edtuserid.getText().toString());
                String sex = "1";
                if(radioButton.isChecked()){
                    sex = "0";
                }
                map.put("sex",sex);
                //创建请求数据类型
                MediaType mediaType = MediaType.parse("application/json");
                OkHttpClient okHttpClient = new OkHttpClient();
                //把注册信息和请求类型封装成请求体
                RequestBody requestBody = RequestBody.create(mediaType,new Gson().toJson(map));
                //通过post请求体发送出去
                Request request = new Request.Builder().url(Configs.Host+APIInterface.RegUser)
                        .post(requestBody).build();
                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    }
                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        postResponse = new Gson().fromJson(response.body().string(),PostResponse.class);
                        handler.sendEmptyMessage(1);
                    }
                });
            }
        });
    }
}